﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Configuration;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
#endregion



namespace NetBay.Core.BusinessEntities.ActiveDirectory
{

    /// <summary>
    /// The Active directory configuratin file structure 
    /// </summary>
    [DataContract]
    [Serializable]
    public class AdStructureItem : IEntity
    {

        /// <summary>
        /// Gets or sets the key id.
        /// </summary>
        /// <value>The key id.</value>
        [DataMember]
        [Key]
        public Guid KeyId { get; set; }

        /// <summary>
        /// Gets or sets the key id.
        /// </summary>
        /// <value>The key id.</value>
        [DataMember]
        public Guid ForeignKey1 { get; set; }

        /// <summary>
        /// Just the name of the AD
        /// </summary>
        /// <value>The name of the ad.</value>
        [DataMember]
        public string AdName { get; set; }

        /// <summary>
        /// Gets or sets the ad PWD.
        /// </summary>
        /// <value>The ad PWD.</value>
        [DataMember]
        public string AdPwd { get; set; }

        /// <summary>
        /// Gets or sets the ad login.
        /// </summary>
        /// <value>The ad login.</value>
        [DataMember]
        public string AdLogin { get; set; }

        /// <summary>
        /// Gets or sets the ad root dc.
        /// </summary>
        /// <value>The ad root dc.</value>
        [DataMember]
        public string AdRootDc { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="AdStructureItem" /> class.
        /// </summary>
        public AdStructureItem()
        {
            KeyId = Guid.NewGuid();
        }

        /// <summary>
        /// Sets the foreign key.
        /// </summary>
        /// <param name="p_ieCollection">The p_ie collection.</param>
        /// <param name="p_strKey">The P_STR key.</param>
        public void SetForeignKey1(IEnumerable<IEntity> p_ieCollection, Guid p_strKey)
        {
            foreach (IEntity l_ieItem in p_ieCollection)
            {
                l_ieItem.ForeignKey1 = p_strKey;
            }
        }
    }


    /// <summary>
    /// Active directory configuration Class
    /// </summary>
    [DataContract]
    [Serializable]
    public class AdConfig : IConfigurationSectionHandler, IEntity
    {
        #region Init/Constructor
        public AdConfig() 
        {
            KeyId = Guid.NewGuid();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the key id.
        /// </summary>
        /// <value>The key id.</value>
        [DataMember]
        [Key]
        public Guid KeyId { get; set; }

        /// <summary>
        /// Gets or sets the key id.
        /// </summary>
        /// <value>The key id.</value>
        [DataMember]
        public Guid ForeignKey1 { get; set; }

        #endregion

        #region Private


        // Contain all AD declaration from the configurtation file
        private List<AdStructureItem> _lstStructure = new List<AdStructureItem>();
        #endregion

        #region Property
        [Association("LstAdStructureItem", "KeyId", "ForeignKey1")]
        [Include]
        [DataMember]
        public List<AdStructureItem> LstAdStructureItem
        {
            get { return _lstStructure; }
            set
            {
                _lstStructure = value;
                SetForeignKey1(_lstStructure, KeyId);
            }
        }
        #endregion

        #region Public 

        /// <summary>
        /// Sets the foreign key.
        /// </summary>
        /// <param name="p_ieCollection">The p_ie collection.</param>
        /// <param name="p_strKey">The P_STR key.</param>
        public void SetForeignKey1(IEnumerable<IEntity> p_ieCollection, Guid p_strKey)
        {
            foreach (IEntity l_ieItem in p_ieCollection)
            {
                l_ieItem.ForeignKey1 = p_strKey;
            }
        }

        #endregion

        #region IConfigurationSectionHandler Members
        /// <summary>
        /// Fill the object with the xlk section
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            try
            {
                var lstAdStructureItemTemp = new List<AdStructureItem>();
                var xmllist = section.SelectNodes("//Ad");
                if (xmllist != null)
                    foreach (System.Xml.XmlNode item in xmllist)
                    {
                        var tempObject = new AdStructureItem();
                        if (item.Attributes != null)
                        {
                            tempObject.AdName = item.Attributes["AdName"].Value;
                            tempObject.AdLogin = item.Attributes["AdLogin"].Value;
                            tempObject.AdPwd = item.Attributes["AdPwd"].Value;
                            tempObject.AdRootDc = item.Attributes["AdRootDc"].Value;
                        }
                        lstAdStructureItemTemp.Add(tempObject);
                    }
                LstAdStructureItem = lstAdStructureItemTemp;
            }
            catch (Exception ex)
            {
                throw new Exception(ToString(), ex);
            }

            return this;
        }

        #endregion
    }

   
}
